package com.cs.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.cs.entity.Admin;
import com.cs.entity.Page;
import com.cs.entity.Student;
import com.cs.entity.Teacher;
import com.cs.service.AdminServiceImpl;
import com.cs.service.TeacherServiceImpl;

@Controller
public class AdminController {
	@Resource
	private AdminServiceImpl aImpl;
	@Resource
	private TeacherServiceImpl tImpl;
	
	//管理员登录
	@RequestMapping("admlogin")
	public Object admLogin(HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		Admin admin = this.aImpl.admLogin(username, password);
		if (admin!=null) {
			request.getSession().setAttribute("admin", admin);
			out.print("<script language='javascript'>alert('登录成功');window.location.href='findAllStudent'</script>");
		}
		out.print("<script language='javascript'>alert('用户名或密码错误');window.location.href='login.jsp'</script>");
		return out;
	}
	//查询全部学生并分页
	@RequestMapping("findAllStudent")
	public String findAllStudent(HttpServletRequest request,Model model) {
		int pageNum = Integer.parseInt(request.getParameter("pageNum")==null||request.getParameter("pageNum").equals("")?"1":request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize")==null||request.getParameter("pageSize").equals("")?"10":request.getParameter("pageSize"));
		Page<Student> sPage = this.aImpl.findAllStudent(pageNum, pageSize);
		model.addAttribute("sPage", sPage);
		return "/stuadmin.jsp";
	}
	//添加学生
	@RequestMapping("addstudent")
	public Object addStudent(Student student,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		//检查学号是否重复
		boolean c = this.aImpl.findStudentBySno(student.getSno());
		if (!c) {
			boolean b = this.aImpl.addStudent(student);
			if (b) {
				writer.print("<script language='javascript'>alert('添加成功');window.location.href='findAllStudent'</script>");
			}
			writer.print("<script language='javascript'>alert('添加失败');window.location.href='addstudent.jsp'</script>");
		}
		writer.print("<script language='javascript'>alert('该学号已存在');window.location.href='findAllStudent'</script>");
		return writer;
	}
	//批量添加学生（根据excel表格）
	@RequestMapping(value="batchaddstudent",method=RequestMethod.POST)
	public Object batchAddStudent(HttpServletResponse response,HttpServletRequest request,@RequestParam("file") MultipartFile file) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		if (!file.isEmpty()) {
			File file2= new File(request.getServletContext().getRealPath("xls"), file.getOriginalFilename());
			file.transferTo(file2);
			FileInputStream f = new FileInputStream(file2);
			Workbook workbook = new XSSFWorkbook(f);
			Sheet sheet=workbook.getSheetAt(0);
			for(Row row:sheet){
				if (row.getRowNum()<1) {
					continue;
				}
				//检查学号与数据库是否重复
				boolean b = this.aImpl.findStudentBySno(row.getCell(2).getStringCellValue());
				if (!b) {
					Student student=new Student();
					student.setSpecialities(row.getCell(0).getStringCellValue());
					student.setSname(row.getCell(1).getStringCellValue());
					student.setSno(row.getCell(2).getStringCellValue());
					student.setSsex(row.getCell(3).getStringCellValue());
					this.aImpl.addStudent(student);
				}else {
					System.out.println("学号："+row.getCell(2).getStringCellValue()+"已存在");
				}
			}
			f.close();
			writer.print("<script language='javascript'>alert('批量添加成功');window.location.href='findAllStudent'</script>");
		}
		writer.print("<script language='javascript'>alert('该文件为空');window.location.href='addstudent.jsp'</script>");
		return writer;
	}
	//删除学生
	@RequestMapping("delstudent")
	public Object delStudent(@RequestParam("id") Integer id,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		boolean b = this.aImpl.delStudent(id);
		if (b) {
			writer.print("<script language='javascript'>alert('删除成功');window.location.href='findAllStudent'</script>");
		}
		writer.print("<script language='javascript'>alert('删除失败');window.location.href='findAllStudent'</script>");
		return writer;
	}
	//查询全部教师并分页
	@RequestMapping("findAllTeacher")
	public String findAllTeacher(HttpServletRequest request,Model model) {
		int pageNum = Integer.parseInt(request.getParameter("pageNum")==null||request.getParameter("pageNum").equals("")?"1":request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize")==null||request.getParameter("pageSize").equals("")?"10":request.getParameter("pageSize"));
		Page<Teacher> tPage = this.aImpl.findAllTeacher(pageNum, pageSize);
		model.addAttribute("tPage", tPage);
		return "/teaadmin.jsp";
	}
	//添加教师
	@RequestMapping("addteacher")
	public Object addStudent(Teacher teacher,HttpServletResponse response) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		//检查教工号是否重复
		boolean c = this.aImpl.findTeacherByTno(teacher.getTno());
		if (!c) {
			boolean b = this.aImpl.addTeacher(teacher);
			if (b) {
				writer.print("<script language='javascript'>alert('添加成功');window.location.href='findAllTeacher'</script>");
			}
			writer.print("<script language='javascript'>alert('添加失败');window.location.href='addteacher.jsp'</script>");
		}
		writer.print("<script language='javascript'>alert('该学号已存在');window.location.href='findAllTeacher'</script>");
		return writer;
	}
	//删除教师
	@RequestMapping("delteacher")
	public Object delTeacher(HttpServletResponse response,@RequestParam("id") Integer id) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		System.out.println("教师id为"+id);
		Teacher teacher = this.tImpl.findTeacherById(id);
		//判断该教师是否有开设课程
		if (teacher.getCourse().size()>0) {
			writer.print("<script language='javascript'>alert('删除失败,该教师已课设了课程');window.location.href='findAllTeacher'</script>");
		}else {
			boolean b = this.aImpl.delTeacher(id);
			if (b) {
				writer.print("<script language='javascript'>alert('删除成功');window.location.href='findAllTeacher'</script>");
			}
		}
		return writer;
	}

}
